
import java.io.IOException;
import java.util.Enumeration;
import java.util.Vector;

import javax.microedition.io.Connector;
import javax.microedition.io.file.FileConnection;

import java.io.InputStream;
import javax.microedition.lcdui.Image;

public class FileUtil { 

    public static Vector GetSDcardContent() throws Exception {
        Vector allContent = new Vector(); 
        FileConnection fc = (FileConnection) Connector.open("file:///SDCard/", Connector.READ);
        Enumeration fileList = fc.list("*", true);
        while (fileList.hasMoreElements()) {
            String name = (String) fileList.nextElement(); 
            fc = (FileConnection) Connector.open("file:///SDCard/" + name);
            File file = new File();
            file.fileName = name;
            if (fc.isDirectory()) {
                file.isDirectory = true;
            } else {
                file.isDirectory = false;
                file.size = fc.fileSize();
            }
            allContent.addElement(file);
        }
        return allContent;
    }

    public static Directory GetContentV2(String path) throws Exception {
        Directory directory = new Directory();
        directory.filePath = path;
        FileConnection fc = (FileConnection) Connector.open(path, Connector.READ);
        Enumeration fileList = fc.list();
        while (fileList.hasMoreElements()) {
            String name = (String) fileList.nextElement();
            fc = (FileConnection) Connector.open(path + name, Connector.READ);
            if (fc.isDirectory()) {
                directory.lstDirectorChild.addElement(name);
            } else {
                directory.lstFileName.addElement(name);
            }
        }
        return directory;
    }

    public static Vector GetContent(String path) throws Exception {
        Vector allContent = new Vector();
        FileConnection fc = (FileConnection) Connector.open(path, Connector.READ);
        Enumeration fileList = fc.list();
        while (fileList.hasMoreElements()) {
            String name = (String) fileList.nextElement();
            fc = (FileConnection) Connector.open(path + name, Connector.READ);
            File file = new File();
            file.fileName = name;
            file.directoryName = path + name;
            if (fc.isDirectory()) {
                file.isDirectory = true;
                file.listChild = GetContent(path + "/" + name);  
            } else {
                file.isDirectory = false;
                file.size = fc.fileSize();
            }
            allContent.addElement(file);
        }
        return allContent;
    }

    public static byte[] ReadFile(String fileName) throws IOException {
        FileConnection fc = (FileConnection) Connector.open(fileName, Connector.READ);
        if (fc.exists()) {
            InputStream is = fc.openInputStream();
            byte[] data = new byte[(int) fc.fileSize()];
            int read = 0;
            while (read != -1) {
                read = is.read(data);
            }
            return data;
        }
        return null;
    }

    public static Image showFile(String fileName) throws Exception {
        Image image = null;
        FileConnection fc = (FileConnection) Connector.open(fileName, Connector.READ);
        if (fc.exists()) {
            InputStream is = fc.openInputStream();
            image = Image.createImage(is);
        }
        return image;
    }
}
